home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Typography Samples / Tracking Layout ƒ / Tracking Layout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-18  |  12.5 KB  |  400 lines  |  [TEXT/KAHL]

  1. /**\
  2. |**| =====================================================================
  3. |**|
  4. |**|    Tracking Layout.c
  5. |**|
  6. |**|    This file contains the calls that this sample needs to make
  7. |**|    the QuickDraw GX shell work correctly.
  8. |**|
  9. |**|    QuickDraw GX Libraries Used:
  10. |**|    "ColorLibrary.c", "FontLibrary.c", "GraphicsDebugLibrary.c",
  11. |**|    "LayoutLibrary.c", "ShapeLibrary.c", and "TransformLibrary.c".
  12. |**|
  13. |**|    6/96 bob    Updated #includes to support changed GX Library names.
  14. |**|                Updated the copyright date.
  15. |**|
  16. |**|    ©1990 - 1996  Apple Computer, Inc.
  17. |**|    All rights reserved.
  18. |**|
  19. |**| =====================================================================
  20. \**/
  21.  
  22.  
  23. #include "QDGX shell.h"
  24. #include <GXLayout.h>
  25. #include "LayoutLibrary.h"
  26.  
  27.  
  28. /**\
  29. |**| ---------------------------------------------------------------------
  30. |**| PROTOTYPES
  31. |**| ---------------------------------------------------------------------
  32. \**/
  33.  
  34. // funtions required by shell
  35.  
  36. void    DoSetup            (void);
  37. void    DoDraw            (WindowPtr wind, Boolean updating);
  38. OSErr    DoCreateNew        (void);
  39. void    DoDispose        (WindowPtr wind);
  40. void    DoIdle            (WindowPtr wind);
  41. void    DoTeardown        (void);
  42. void    DoClick            (WindowPtr wind, Point p);
  43.  
  44. // private functions
  45.  
  46. OSErr    DoWindowInit        (WindowPtr wind);
  47. void    CreateSampleImage    (WindowPtr wind);
  48.  
  49.  
  50. /**\
  51. |**| ---------------------------------------------------------------------
  52. |**| ENUMS
  53. |**| ---------------------------------------------------------------------
  54. \**/
  55. enum { rWindResource = 128 };
  56.  
  57.  
  58. /**\
  59. |**| ---------------------------------------------------------------------
  60. |**| GLOBALS
  61. |**| ---------------------------------------------------------------------
  62. \**/
  63. // If gDebugging = TRUE, graphics library errors and notices will be posted.  This
  64. // functionality will only work with the "debugging" version of QuickDraw GX.
  65. // If the debugging version is not installed, nothing bad will happen, but these
  66. // functions will not work. 
  67.  
  68. Boolean        gDebugging = true;
  69.  
  70. // Set  "gGiveMeValidation" to TRUE if you want receive run-time validation.
  71.  
  72. Boolean        gGiveMeValidation = true;
  73.  
  74.  
  75. // gGraphicsHeapSize sets the size of the graphics heap created by calling the
  76. // GXNewGraphicsClient routine in main () within QuickDraw GX shell.c.  You can determine
  77. // the amount of graphics heap required by using GraphicsBug.  I'm giving it 600K,
  78. // since we need abunch if we have several windows open.
  79.  
  80. long        gGraphicsHeapSize = 600;
  81.  
  82. // gOurPrintingOverrideUPP is a universal proc pointer for our printing event
  83. // override.  This is so that our override can be native PowerPC code if necessary.
  84.  
  85. GXPrintingEventUPP    gOurPrintingOverrideUPP;
  86.  
  87.  
  88.  
  89. /**\
  90. |**| ---------------------------------------------------------------------
  91. |**| DoSetup()
  92. |**| Here's where we initialize any global variables our application needs.
  93. |**| We have only one at this time -- the universal proc pointer for
  94. |**| our printing override.
  95. |**| ---------------------------------------------------------------------
  96. \**/
  97. void DoSetup (void)
  98. {    // Initialize our printing event override UPP
  99.     gOurPrintingOverrideUPP = NewGXPrintingEventProc(MyPrintingEventOverride);
  100. }
  101.  
  102.  
  103. /**\
  104. |**| ---------------------------------------------------------------------
  105. |**| DoDraw()
  106. |**| Draw the contents of the window.  The first parameter is the window
  107. |**| to draw, and the second parameter is true if we're updating an existing
  108. |**| image.  If that's the case, we don't want to change anything, but
  109. |**| just draw what's already there.
  110. |**| ---------------------------------------------------------------------
  111. \**/
  112. void DoDraw (WindowPtr wind, Boolean updating)
  113. {
  114.      #pragma unused (updating)
  115.      GXDrawShape (GetDocShape(wind));
  116. }
  117.  
  118.  
  119. /**\
  120. |**| ---------------------------------------------------------------------
  121. |**| DoCreateNew()
  122. |**| This routine is called when a window needs to be created.
  123. |**| ---------------------------------------------------------------------
  124. \**/
  125. OSErr DoCreateNew (void)
  126. {
  127.     OSErr        err = noErr;
  128.     WindowPtr    wind;
  129.     
  130. // Get and create our window from the resource fork
  131.  
  132.     wind = GetNewWindow(rWindResource, nil, (WindowPtr)-1L);
  133.  
  134. // Attach a default gxViewPort to it, create and iInitialize our
  135. // private data for it, and add a sample image to its page shape.
  136.  
  137.     if ( wind == NULL )
  138.         return (MemError());
  139.  
  140.     GXIgnoreGraphicsNotice(transform_already_set);
  141.     SetDefaultViewPort(GXNewWindowViewPort(wind));            
  142.     GXPopGraphicsNotice();
  143.     
  144.     err = DoWindowInit(wind);
  145.     if ( err != noErr )
  146.         return err;
  147.     
  148.     CreateSampleImage(wind);
  149.     return err;
  150. }
  151.  
  152.  
  153. /**\
  154. |**| ---------------------------------------------------------------------
  155. |**| DoDispose()
  156. |**| This routine is called when a window needs to be disposed of.
  157. |**| ---------------------------------------------------------------------
  158. \**/
  159. void DoDispose (WindowPtr wind)
  160. {
  161.     TH_Doc    doc;
  162.     
  163. // You should always dispose of your GX graphics objects before tossing your window.
  164. // Why?  It's generally good form and this approach guarantees that everything is
  165. // disposed.  If you had not disposed of everything, the call to DisposeWindow should
  166. // dispose of the objects. If you are running the debugging version of QuickDraw GX
  167. // with notices set, you will receive a notice that you had not disposed of everything.
  168. // You can turn notices on in this file by setting gDebugging = TRUE (above).
  169.     
  170.     if ( wind != NULL )
  171.     {
  172.         doc = (TH_Doc)GetWRefCon(wind);        // Remember, this is where we stored our private data.
  173.         GXDisposeShape(GetDocShape(wind));     // Dispose of this doc's shape.
  174.         GXDisposeJob(GetDocJob(wind));        // Dispose of this doc's print job.
  175.         DisposHandle((Handle) doc);            // Dispose of our private data.
  176.         DisposeWindow(wind);                // Dispose of the window.
  177.     }
  178. }
  179.  
  180.  
  181. /**\
  182. |**| ---------------------------------------------------------------------
  183. |**| DoIdle()
  184. |**| This routine is called to do things while idling through the event loop.
  185. |**| ---------------------------------------------------------------------
  186. \**/
  187. void DoIdle (WindowPtr wind)
  188. {
  189. }
  190.  
  191.  
  192. /**\
  193. |**| ---------------------------------------------------------------------
  194. |**| DoTeardown()
  195. |**| This routine is called just before we quit to remove anything 
  196. |**| persistent that might have been setup by DoSetup().
  197. |**| ---------------------------------------------------------------------
  198. \**/
  199. void DoTeardown (void)
  200. {
  201.     DisposeRoutineDescriptor(gOurPrintingOverrideUPP);
  202. }
  203.  
  204.  
  205. /**\
  206. |**| ---------------------------------------------------------------------
  207. |**| DoClick()
  208. |**| ---------------------------------------------------------------------
  209. \**/
  210. void DoClick(WindowPtr window, Point p)
  211. {
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. /**\
  219. |**| ---------------------------------------------------------------------
  220. |**| DoWindowInit()
  221. |**| In this function we create and initialize the the private document
  222. |**| structure for a new window.  This structure contains the print job and
  223. |**| the shape which is drawn in the window.  We store this data in a handle
  224. |**| and hang it off the window's refCon field for easy retrieval.  By doing
  225. |**| this, rather than using globals, we can create many windows containing
  226. |**| unique print jobs and shapes.
  227. |**| ---------------------------------------------------------------------
  228. \**/
  229. OSErr DoWindowInit (WindowPtr wind)
  230. {
  231.     OSErr    err = noErr;
  232.     gxJob    docJob;
  233.     gxShape    docPage;
  234.     TH_Doc    windDoc;
  235.  
  236.  
  237. // Create the page shape. We set the unique items attribute to make sure that each item
  238. // added to the picture has a unique reference. If this attribute was not set, we would
  239. // not see all copies of anything we add to the shape multiple times -- we'd just see
  240. // the last version added.        
  241.  
  242.     docPage = GXNewShape(gxPictureType);
  243.     GXSetShapeAttributes(docPage, (GXGetShapeAttributes(docPage) | gxUniqueItemsShape));
  244.     
  245.     
  246. // Create a print job for this document.  This will be the same as the system default until
  247. // the user goes through the dialogs for Page Setup or Print…
  248.  
  249.     err = GXNewJob(&docJob);
  250.     
  251.     
  252. // If there are no errors, create a handle the size of our document structure and store
  253. // the print job and page shape in it.  Store the handle in the window's refCon field so
  254. // that we can get at it.  (Note that the utility routines "GetDocJob" and "GetDocShape"
  255. // can be used to do this easily.
  256.  
  257.     if ( err == noErr )
  258.     {
  259.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  260.  
  261.         if ( windDoc == NULL )
  262.             err = MemError();
  263.         else
  264.         {
  265.             (*windDoc)->docJob = docJob;
  266.             (*windDoc)->docPage = docPage;
  267.             SetWRefCon(wind, (long) windDoc);
  268.         }
  269.  
  270. // Now install our application override for PrintingEvent so that we can
  271. // support the new movable-modal printing dialog boxes.
  272.  
  273.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, gOurPrintingOverrideUPP);
  274.  
  275.     }
  276.  
  277.     return err;
  278. }
  279.  
  280.  
  281. /**\
  282. |**| ---------------------------------------------------------------------
  283. |**| CreateSampleImage()
  284. |**| This function creates primitive shapes and adds them to the window's page shape.
  285. |**| ---------------------------------------------------------------------
  286. \**/
  287. void CreateSampleImage (WindowPtr wind)
  288. {
  289.     Rect    ourWindowRect = wind->portRect; // the rectangle for this window
  290.                                             // in QuickDraw coordinates
  291.     gxShape layoutNormal;                // the regular layout shape we build
  292.     gxShape layoutTight;                // a layout with tighter tracking
  293.     gxShape layoutLoose;                // a layout with looser tracking
  294.     gxShape thePage;                    // this window's document shape
  295.     gxRunControls runControls;            // run controls for the layout shape
  296.     gxLayoutOptions layoutOptions;        // options for the layout shape
  297.     
  298. // These are the five text runs for this layout shape, in pieces because they're
  299. // in different languages
  300.     
  301.     char *text = "track track track track track";
  302.     
  303.     gxPoint posn;                        // the position of the layout shape
  304.  
  305.  
  306. // make default gxLayoutOptions and gxRunControls structures
  307.  
  308.     InitializeLayoutOptions (&layoutOptions);
  309.     InitializeRunControls (&runControls);
  310.     
  311.     
  312. // Position the layout half way down the left edge of the window. Set 
  313. // the layout's width to the window's width and set the flushness to 1/2, this
  314. // will cause the layout to center in the window.  Note that ourWindowRect is
  315. // not in fixed coordinates, so we have to make it fixed to use it.
  316.  
  317.     layoutOptions.width = ff(ourWindowRect.right - ourWindowRect.left);
  318.     layoutOptions.flush = fract1/2;
  319.  
  320.     posn.x = 0;
  321.     posn.y = ff((ourWindowRect.bottom - ourWindowRect.top) / 2);
  322.     
  323.  
  324. // Create the layouts we'll use.  NewSingleLayout is a library routine found
  325. // in layout library.c to create a layout based on one style.
  326.  
  327.     layoutNormal = NewSingleLayout(
  328.                 text,                             // the text for the layout
  329.                 (char *) "\pHoefler Text",         // the font name to use
  330.                 ff(32),                         // the point size to use
  331.                 &layoutOptions,                 // our default layout options
  332.                 &posn,                             // the shape's position
  333.                 0,                                 // attributes (none)
  334.                 &runControls,                    // our run controls
  335.                 nil,                             // the run features array (none)
  336.                 0,                                 // count of zero features = 0
  337.                 nil);                            // no style run overrides
  338.                 
  339.     posn.y -= ff(40);                            // move up forty points
  340.     
  341. // Now adjust the tracking to be very tight per the font designer's choices
  342.  
  343.     runControls.track = ff(-2);
  344.     
  345.     layoutTight = NewSingleLayout(
  346.                 text,                             // the text for the layout
  347.                 (char *) "\pTimes Roman",         // the font name to use
  348.                 ff(32),                         // the point size to use
  349.                 &layoutOptions,                 // our default layout options
  350.                 &posn,                             // the shape's position
  351.                 0,                                 // attributes (none)
  352.                 &runControls,                    // our run controls
  353.                 nil,                             // the run features array (tight)
  354.                 0,                                 // count of zero features = 0
  355.                 nil);                            // no style run overrides
  356.                 
  357.     posn.y += ff(80);                            // now draw forty points below center
  358.     
  359.     
  360. // Now adjust the tracking to be very loose per the font designer's choices
  361.  
  362.     runControls.track = ff(2);
  363.     
  364.     layoutLoose = NewSingleLayout(
  365.                 text,                             // the text for the layout
  366.                 (char *) "\pTimes Roman",         // the font name to use
  367.                 ff(32),                         // the point size to use
  368.                 &layoutOptions,                 // our default layout options
  369.                 &posn,                             // the shape's position
  370.                 0,                                 // attributes (none)
  371.                 &runControls,                    // our run controls
  372.                 nil,                             // the run features array (loose)
  373.                 0,                                 // count of zero features = 0
  374.                 nil);                            // no style run overrides
  375.                 
  376.     
  377. // Retrieve the page shape so we can add to it.
  378.  
  379.     thePage = GetDocShape(wind);
  380.     
  381. // Add the layout to the window's picture shape.  AddToShape is in shape library.c.
  382.  
  383.     AddToShape(thePage, layoutNormal);
  384.     AddToShape(thePage, layoutTight);
  385.     AddToShape(thePage, layoutLoose);
  386.  
  387. // Now that all shapes are added to the picture, dispose of them
  388.  
  389.     GXDisposeShape(layoutNormal);
  390.     GXDisposeShape(layoutTight);
  391.     GXDisposeShape(layoutLoose);
  392.     
  393.  
  394. // Invalidate the window's portRect so that everything gets updated.
  395.     
  396.     SetPort(wind);
  397.     InvalRect(&ourWindowRect);
  398. }
  399.  
  400.